/**
 * Filename: d:\CODE\FTP\FTP_TEST\FTP\static\js\page_4.js
 * Path: d:\CODE\FTP\FTP_TEST\FTP\static\js
 * Created Date: Wednesday, May 11th 2022, 4:21:03 pm
 * Author: xiguan
 * 
 * Copyright (c) 2022 XiGuan Inc.
 */


const basic_url = "http://127.0.0.1:8880";

let current_syncdir_id = 0; // 当前同步目录编号
let current_server_port = 22; //当前监听端口
let current_interclock_id = 0; //当前同步定时器id

// 封装ajax请求

function ajax(params){
    let url = basic_url+params.url;
    let data = params.data;
    let method = "GET";
    if(params.method !== ""){
        method = params.method;
    }

    return $.ajax({
        url : url,
        data : data,
        method: method
    });
}
async function get_all_synclog() {
    
    let params = {
        url: "/log/get_all_synclog/"
    };
    ajax(params).then(async data => {
        let logs = JSON.parse(data.logs)
        if(data.status === "OK") {
            $("#content4-sync-history").html("");
            let str =   '<table class="table">' +
                        '<thead>' +
                            '<tr>' +
                                '<th>编号</th>' +
                                '<th>用户名</th>' +
                                '<th>侦听端口</th>' +
                                '<th>同步方向</th>' +
                                '<th>同步路径</th>' +
                                '<th>同步编号</th>' +
                                '<th>同步时间</th>' +
                                '<th>删除</th>' +
                            '</tr>' +
                        '</thead>' +
                        '<tbody>';
            for(let i=0; i<logs.length; i++) {
                if(i%2 === 0) {
                    str += '<tr class="info">';
                } else {
                    str += '<tr class="active">'
                }
                str +=          '<td>'+ Number(i+1) +'</td>' +
                                '<td>'+ logs[i].username +'</td>' +
                                '<td>'+ logs[i].server_port +'</td>' +
                                '<td>'+ logs[i].direction +'</td>' +
                                '<td>'+ logs[i].syncdir_path +'</td>' +
                                '<td>'+ logs[i].syncdir_id +'</td>' +
                                '<td>'+ logs[i].sync_time +'</td>' +
                                '<td><a href="#" onclick="content4_log_del(`' + logs[i].id +'`) " id="content4-log-del-icon"><span class="glyphicon-minus"></span></a></td>' +
                            '</tr>';
            }
            str +=  '</tbody>' +
                    '</table>';
            $('#content4-sync-history').append(str);
        }
    });
}

function content4_log_del(log_id) {
    // 确认框
    bootbox.confirm({
        message: "确认删除吗?",
        buttons: {
                confirm: {
                    label: '确定',
                },
                cancel: {
                    label: '取消',
                }
        },
        callback: async function (result) {
            if (result) {
                let params = {
                    url: "/log/del_log/",
                    data: {
                        "id": log_id
                    }
                };
                ajax(params).then(async data => {
                    if(data === "OK") {
                        await get_all_synclog();
                    }
                })
            }
        }
    });
}

$(async function(){

    get_all_synclog();

    $("#content4-log-clear-btn").click(async () => {
        // 确认框
        bootbox.confirm({
            message: "确认清空日志吗?",
            buttons: {
                    confirm: {
                        label: '确定',
                    },
                    cancel: {
                        label: '取消',
                    }
            },
            callback: async function (result) {
                if (result) {
                    var dialog = bootbox.dialog({
            message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i>正在清空日志，请稍等...</p>',
            closeButton: false
        });
                    let params = {
                        url: "/log/clear_log/",
                    };
                    ajax(params).then(data => {
                        if(data === "OK") {
                            setTimeout(async function (){
                                dialog.modal('hide');
                                await get_all_synclog();
                            },2000);
                        } else {
                            setTimeout(async function (){
                                dialog.modal('hide');
                            },2000);
                        }
                    })
                }
            }
        });

    });

})